package com.mouse.jhttp.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;

public class JsonUtil {

    private static final ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    public static <T> String toJson(T data) {
        try {
            return mapper.writeValueAsString(data);
        } catch (Exception e) {

            throw new RuntimeException(e);
        }
    }

    public static <T> T getObject(String param, Class<T> t) {
        //设置忽略多余字段
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            return mapper.readValue(param, t);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
